﻿using System;
using System.Collections.Generic;
using System.Data;

namespace NTL.DAL
{
	public abstract class ImagesProvider : DataAccess
	{
		static private ImagesProvider _instance = null;
		static public ImagesProvider Instance
		{
			get
			{
				if (_instance == null)
					_instance = (ImagesProvider)Activator.CreateInstance(
					   Type.GetType("NTL.DAL.SqlClient.SqlImagesProvider"));
				return _instance;
			}
		}

		public ImagesProvider()
		{
			this.ConnectionString = Globals.Settings.ConnectionString;
			this.EnableCaching = Globals.Settings.EnableCaching;
			this.CacheDuration = Globals.Settings.CacheDuration;
		}

		public abstract ImageDetails FindById(long galleryId, long imageId);
		public abstract List<ImageDetails> FindByTimeline(int year, long pageIndex, long pageSize);
		public abstract List<ImageDetails> FindByTimeline(int year, int month, long pageIndex, long pageSize);
		public abstract List<ImageDetails> FindByTimeline(int year, int month, int day, long pageIndex, long pageSize);
		public abstract long CountByTimeline(int year);
		public abstract long CountByTimeline(int year, int month);
		public abstract long CountByTimeline(int year, int month, int day);

		public abstract long Insert(long galleryId, ImageDetails image);
		public abstract long Update(long galleryId, ImageDetails image);

		protected virtual ImageDetails GetFromReader(IDataReader reader)
		{
			return new ImageDetails(
				(long)reader["GalleryId"],
				(long)reader["ImageId"],
				(DateTime)reader["AddedDate"],
				(DateTime)reader["Timeline"],
				reader["Name"].ToString(),
				reader["Url"].ToString(),
				(bool)reader["Visible"]);
		}
		protected virtual List<ImageDetails> GetCollectionFromReader(IDataReader reader)
		{
			List<ImageDetails> images = new List<ImageDetails>();
			while (reader.Read())
				images.Add(GetFromReader(reader));
			return images;
		}
	}
}
